/**
 * 数据列表页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_OPT_ORG_CONTRACT");
    initSelect2();
    initLayDate("failureDataBegin", "date");
    initLayDate("failureDataEnd", "date");
    //加载数据列表
    loadGrid();
    $("#queryBtn").click(function(){
        query();
    });
    $("#resetBtn").click(function(){
        $('#searchForm')[0].reset();
        $('#searchForm').find('.select2-chosen').html('请选择');
    });
    $("#addBtn").click(function(){
        openAddPage();
    });
});

/**
 * 加载数据列表
 */
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype: "POST",
        url: springUrl+"/web/orgContract/page",
        datatype: "json",
        autowidth: true,
        height: "100%",
        colModel: [
            {name:'uniqueNo', label:'uniqueNo', index:'', width:50, sortable: false, resizable:false, hidden: true},
            {name:'orgName',label:'机构名称', index:'',width:100, sortable: false, align:'left'},
            {name:'contractTypeName',label:'合同类型', index:'', width:100, sortable: false, align:'left'},
            {name:'contractAmount',label:'合同金额', index:'', width:100, sortable: true, align:'left'},
            {name:'effectiveData',label:'生效时间', index:'', width:100, sortable: true, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue !== null){
                        return moment(cellValue).format("YYYY-MM-DD");
                    }else{
                        return "";
                    }
                }},
            {name:'failureData',label:'失效时间', index:'', width:100, sortable: true, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue !== null){
                        return moment(cellValue).format("YYYY-MM-DD");
                    }else{
                        return "";
                    }
                }},
            {name:'', label:'操作', index:'', width:220, fixed:true, sortable:false, resize:false, align:'center',
                formatter:function(cellValue, options, rowObject){
                    var context = "";
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openEditPage(\''+rowObject.uniqueNo+'\')" title="修改">修改</button> ';
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="deleteByPrimaryKey(\''+rowObject.uniqueNo+'\')" title="删除">删除</button>';
                    return context;
                }
            }
        ],
        rownumbers: true,//添加左侧行号
        viewrecords : true,
        rowNum: 10,
        rowList: [10,20,30],
        sortname: "create_time",
        sortorder: "desc",
        altRows: true,
        pager : "#gridPager",
        jsonReader: { //server返回Json解析设定
            root: "content", //对于json中数据列表
            page: "pageNum",
            total: "total",
            records: "records"
        },
        gridComplete: function (xhr) {
            var ids = $("#gridTable").getDataIDs();
            for(var i=0;i<ids.length;i++){
                var rowData = $("#gridTable").getRowData(ids[i]);
                var day = Math.floor((new Date(rowData.failureData).getTime()- new Date().getTime())/(24*3600*1000));
                if(day < 0){
                    $('#'+ids[i]).find("td").addClass("bg-red");
                }else if(day < 30){
                    $('#'+ids[i]).find("td").addClass("bg-yellow");
                }
            }
        }
    });
    //navButtons
    gridTable.jqGrid('navGrid',"#gridPager",
        {
            add: false,
            addicon : 'fa-plus purple',
            edit: false,
            editicon : 'fa-pencil blue',
            del: false,
            delicon : 'fa-trash red',
            view: false,
            viewicon : 'fa-search-plus orange',
            search: false,
            searchicon : 'fa-search grey',
            refresh: true,
            refreshicon : 'fa-refresh green'
        }
    );
    updatePagerIcons(this);
    resizeJqGrid("gridTable");
}

/**
 * 查询
 */
function query(){
    if($('#searchForm').trigger("validate").isValid()){
        var gridParam = {
            url: springUrl+"/web/orgContract/page",
            postData:{
                orgNo : $("#orgNo").val(),
                contractTypeCode : $("#contractTypeCode").val(),
                failureDataBegin : $("#failureDataBegin").val(),
                failureDataEnd : $("#failureDataEnd").val()
            },
            page:1
        };
        $("#gridTable").jqGrid("setGridParam", gridParam).trigger("reloadGrid");
    }
}

/**
 * 打开新增页面
 */
function openAddPage(){
    window.forward(springUrl+"/web/orgContract/addPage");
}

/**
 * 打开修改页面
 */
function openEditPage(uniqueNo){
    window.forward(springUrl+'/web/orgContract/editPage?uniqueNo='+uniqueNo);
}

/**
 * 根据主键删除
 */
function deleteByPrimaryKey(uniqueNo) {
    Alert.confirm("您确定删除该条信息？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/orgContract/delete",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}